﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WeatherReader;
using System.Globalization;

public partial class AnnualData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadCombo();
            LoadMonthlyData(DateTime.Today);
        }
    }

    private void LoadCombo()
    {
        var weatherDb = new WeatherDbDataContext();
        var years = from raw in weatherDb.HouseholdDatas
                     group raw by new { raw.Year, } into yearData
                    select new { yearData.Key.Year, MinDay = yearData.Min(m => m.Date) }
                      ;

        cboDate.DataSource = years.OrderByDescending(m => m.MinDay);
        cboDate.DataValueField = "MinDay";
        cboDate.DataTextField = "MinDay";
        //cboDate.DataTextFormatString = "{y}";
        cboDate.DataBind();

    }


    private void LoadMonthlyData(DateTime dayInMonth)
    {
        int month = dayInMonth.Month;
        int year = dayInMonth.Year;

        Page.Title = string.Format("{0} CO2 emissions by month", year);

        var weatherDb =  new WeatherDbDataContext();

        lblMonth.Text = string.Format("CO2 emissions for {0}", dayInMonth.ToString("yyyy"));

        var annualReadings = (from h in weatherDb.HouseholdDatas
                              where (h.Year == year)
                              group h by h.Month into annual
                              select new
                              {
                                  Month = annual.Key,
                                  
                                  ElectricityCo2 = annual.Sum(h => h.ElectricityCo2)/1000,
                                  ElectricityKwh = annual.Sum(h => h.ElectricityKwh) ,
                                  GasCo2 = annual.Sum(h => h.GasCo2)/1000,
                                  GasKwh = annual.Sum(h => h.GasKwh) ,
                                  CO2 = annual.Sum(h=>h.Co2)/1000,
                                  MinDate = annual.Min(h=>h.Date),
                                  EnergyRate =(annual.Sum(h => h.GasKwh) + annual.Sum(h => h.ElectricityKwh)) /(  annual.Count()*24)

                              });

        chartCo2.DataSource = annualReadings;
        chartCo2.ChartAreas["ChartArea1"].AxisX.Maximum = 12;
        chartCo2.DataBind();

        dgData.DataSource = annualReadings;
        dgData.DataBind();



        var annualWeatherReadings = (from h in weatherDb.AllDailyWeatherDatas
                              where (h.Year == year)
                              group h by h.Month into annual
                              select new
                              {
                                  Month = annual.Key,
                                  MinTemp = annual.Min(h => h.MinTemp),
                                  AvgTemp = annual.Average(h => h.AvgTemp),
                                  MaxTemp = annual.Max(h => h.MaxTemp),
                                  DegreeDays = annual.Sum(h => h.DegreeDays)          

                              });
        chartTemp.DataSource = annualWeatherReadings;
        chartTemp.DataBind();

        chartDegreeDays.DataSource = annualWeatherReadings;
        chartDegreeDays.DataBind();

    }
    protected void cboDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        DateTime dtReadingDate = DateTime.Parse(cboDate.SelectedValue, CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat);
        LoadMonthlyData(dtReadingDate);
    }
}
